package service;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.junit.Test;

import bean.Customer;
import bean.GoodDetails;
import util.FpUtil;
import util.khxxUtil;

public class FpService {
	
	/**
	 * 根据折扣计算折扣率
	 * @param discount
	 * @param customer
	 * @return
	 */
	public static Customer updateByDiscount(String discount, Customer customer) {
		if(discount.equals("")||Double.parseDouble(discount)<=0||discount ==null){
			List<GoodDetails> temp = new ArrayList<GoodDetails>();
			for (GoodDetails goodDetails : customer.getGoodDetails()) {
				//1、不为负数商品
				if(new BigDecimal(goodDetails.getAmount()).compareTo(new BigDecimal(0))==1){
					temp.add(goodDetails);
				}
			}
			customer.setGoodDetails(temp);
		}else{
			// 折扣率
			BigDecimal dis = new BigDecimal(discount).divide(new BigDecimal(customer.getSumAmount())
					, 8, BigDecimal.ROUND_HALF_EVEN);
			List<GoodDetails> temp = new ArrayList<GoodDetails>();
			for (GoodDetails goodDetails : customer.getGoodDetails()) {
				//分摊计算折扣金额
				//1、克隆一个同一个商品
				if(new BigDecimal(goodDetails.getAmount()).compareTo(new BigDecimal(0))==-1){
					continue;
				}
				GoodDetails disGood = goodDetails.clone();
				//2、计算此商品的折扣金额
				BigDecimal disMon = new BigDecimal(disGood.getAmount()).multiply(dis).setScale(2, BigDecimal.ROUND_HALF_UP);
				//3、将折扣金额设为负数并正-负依次加入list
				disGood.setAmount(disMon.multiply(new BigDecimal(-1)).toString());
				temp.add(goodDetails);
				temp.add(disGood);
			}
			customer.setGoodDetails(temp);
		}
		return customer;
	}
	
	/**
	 * 更新税号
	 * @param customer
	 * @param row
	 */
	public static void updateSh(Customer customer, int row) {
    	HashMap<String,String> msg = new HashMap<String,String>();
    	msg.put("GFMC" , customer.getName()==null?"":customer.getName());
    	msg.put("AREA" , customer.getArea()==null?"":customer.getArea());
    	msg.put("LXR" , customer.getLinkman()==null?"":customer.getLinkman());
    	msg.put("NSRSBH" , customer.getSh()==null?"":customer.getSh());
    	msg.put("BZ" , customer.getBz()==null?"":customer.getBz());
    	msg.put("DZDH" , customer.getDzdh()==null?"":customer.getDzdh());
    	msg.put("BANK" , customer.getYhzh()==null?"":customer.getYhzh());
    	khxxUtil.updateMsg(customer.getName(), msg);
	}
	
	/**
	 * 单行超限拆分
	 * @param goodDetails 商品信息
	 * @param limit 限额
	 * @return
	 */
	public static List<GoodDetails> splitLine(GoodDetails goodDetails, BigDecimal limit) {
		//单价*数量与限额比较
		Double num = Double.parseDouble(goodDetails.getNum());
		List<GoodDetails> splitList = new ArrayList<GoodDetails>();
		for (int i = 0; i < num; i++) {
			BigDecimal sum = new BigDecimal(i).multiply(new BigDecimal(goodDetails.getPrice())).setScale(2, BigDecimal.ROUND_HALF_UP); 
			if(sum.compareTo(limit) == 1){
				//大的
				BigDecimal sumTemp = new BigDecimal(i-1).multiply(new BigDecimal(goodDetails.getPrice())).setScale(2, BigDecimal.ROUND_HALF_UP); 
				GoodDetails cloneGood = goodDetails.clone();
				cloneGood.setNum(i-1+"");
				cloneGood.setAmount(sumTemp.toString());
				
				//小的
				goodDetails.setNum(num-(i-1)+"");
				goodDetails.setAmount(new BigDecimal(goodDetails.getAmount()).subtract(sumTemp).toString());
				//不包含原来的折扣金额
				goodDetails.setDiscount("");
				splitList.add(cloneGood);
				splitList.addAll(splitLine(goodDetails, limit));
				break;
			}else{
				//最后一个商品的时候
				if(num - i <= 1){
					splitList.add(goodDetails);
				}
			}
		}
		return splitList;
	}
	

	@Test
	public void test(){
		GoodDetails good = new GoodDetails();
		BigDecimal limit = new BigDecimal(99999.99);
		good.setAmount("142572");
		good.setNum("200");
		good.setPrice("1200");
		List<GoodDetails> splitLine = splitLine(good, limit);
		System.out.println(splitLine);
	}
	
	/**
	 * 多行拆分
	 * @param zsDetail
	 * @param limit
	 * @return
	 */
	public static List<List<GoodDetails>> splitTotal(List<GoodDetails> zsDetail, BigDecimal limit) {
		List<List<GoodDetails>> result = new ArrayList<List<GoodDetails>>();
		BigDecimal sumTemp = new BigDecimal(0);
		List<GoodDetails> temp1 = new ArrayList<GoodDetails>();
		for (int i = 0; i < zsDetail.size(); i++) {
			sumTemp = sumTemp.add(new BigDecimal(zsDetail.get(i).getAmount()));
			if(sumTemp.compareTo(limit) != 1){
				temp1.add(zsDetail.get(i));
			}else{
				result.add(temp1);
				temp1 = new ArrayList<GoodDetails>();
				temp1.add(zsDetail.get(i));
				sumTemp = new BigDecimal(zsDetail.get(i).getAmount());
			}
			//最后一个
			if(i == zsDetail.size()-1){
				result.add(temp1);
			}
		}
		return result;
	}
	
	/**
	 * 计算当前集合的总金额
	 * @param list
	 * @return
	 */
	public static String getSumAmount(List<GoodDetails> list) {
		BigDecimal temp = new BigDecimal(0);
		for (GoodDetails goodDetails : list) {
			temp = temp.add(new BigDecimal(goodDetails.getAmount()));
		}
		return temp.toString();
	}
	
	/**
	 * 清除已成功开票和已存在的票据
	 * @param result
	 * @return
	 */
	public static List<Customer> clearRepeat(List<Customer> result) {
		List<Customer> temp = new ArrayList<Customer>();
		//读取文件时先读取备份文件中是否存在以生成xml数据
		List<String> bcList = FpUtil.readData();
		//读取temp中的数据
		List<Customer> tempList = FpUtil.readDataTemp();
		for (Customer customer : tempList) {
			bcList.add(customer.getBillCode());
		}
		for (Customer customer : result) {
			if(!bcList.contains(customer.getBillCode())&&customer.getGoodDetails().size()!=0){
				temp.add(customer);
			}
		}
		return temp;
	}
	
	/**
	 * 根据单据号查询开票结果
	 * @param name
	 * @return
	 */
	public static String FpResult(String name){
		String path = "txt/发票号"+name+"_开票结果.TXT";
		try{
			InputStream inputStream=new FileInputStream(path);
			@SuppressWarnings("resource")
			BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,"GBK"));
			String s = null;
			List<String> list = new ArrayList<String>();
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
            	list.add(s);
            }
            return list.get(list.size()-1);
		} catch (IOException e) {
			return "没有找到文件";
		}
	}
	
	
	@Test
	public void testResult(){
		FpResult("SS-104-2017-08-15-002");
	}
}
